/**
 * @description: Description
 * @author: lizlong<94648929@qq.com>
 * @since: 2021-03-15 21:47:18
 * @LastAuthor: lizlong
 * @lastTime: 2021-03-16 15:59:28
 */
// 引入
import { getTopLeft, getWidth } from "ol/extent";
import TileLayer from "ol/layer/Tile";
import WMTS from "ol/source/WMTS";
import WMTSTileGrid from "ol/tilegrid/WMTS";
import { get } from "ol/proj";

// 全局变量
let key = "829360a00ab4aefa2c81922c5f657c62"
let projection = get("EPSG:4326");
let projectionExtent = projection.getExtent();
let size = getWidth(projectionExtent) / 256;


/**
 \* 私有方法,分辨率
 */
function getResolutions() {
  let resolutions = [];
  for (let z = 0; z <= 20; ++z) {
    resolutions[z] = size / Math.pow(2, z);
  }
  return resolutions
}

export function getProjection() {
  return projection
}

// WMTS 形式
function getWMTSLayer(url, layer) {
  return new TileLayer({
    source: new WMTS({
      name: "中国",
      url: url,
      layer: layer,
      style: "default",
      matrixSet: "c",
      format: "tiles",
      wrapX: true,
      tileGrid: new WMTSTileGrid({
        origin: getTopLeft(projectionExtent),
        resolutions: getResolutions(),
        matrixIds: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
      }),
    }),
  })
}
// WMTS 形式
// function GetFeature() {
//   return new TileLayer({
//     source: new WMTS({
//       name: "中国",
//       url: "http://gisserver.tianditu.gov.cn/TDTService/wfs?request=GetFeature&version=1.0.0&typeName=HYDA",
//     }),
//   })
// }

/**
 \* 矢量底图 + 矢量注记
 */
export function vec_c() {
  return [
    getWMTSLayer('http://t7.tianditu.gov.cn/vec_c/wmts?tk=' + key, 'vec'),
    getWMTSLayer('http://t7.tianditu.gov.cn/cva_c/wmts?tk=' + key, 'cva')
  ]
}

/**
 \* 影像底图 + 影像注记
 */
export function img_c() {
  return [
    getWMTSLayer('http://t7.tianditu.gov.cn/img_c/wmts?tk=' + key, 'img'),
    getWMTSLayer('http://t7.tianditu.gov.cn/cia_c/wmts?tk=' + key, 'cia'),
    // GetFeature('http://gisserver.tianditu.gov.cn/TDTService/wfs?tk=' + key, 'HYDA')
  ]
}